#include "characterclass.h"


#include <QString>


CharacterClass::CharacterClass(const QString &name, QObject *parent) :
    DTwentyItem(name, parent),
    d(new Data())
{
}

CharacterClass::CharacterClass(const CharacterClass &other) :
    DTwentyItem(other),
    d(other.d)
{
}

int CharacterClass::hitDie() const
{
    if (d->hitDie.isNull())
    {
        return -1;
    }
    return d->hitDie.toInt();
}

void CharacterClass::setHitDie(int hitDie)
{
    d->hitDie = hitDie;
}

int CharacterClass::skillPoints() const
{
    if (d->skillPoints.isNull())
    {
        return -1;
    }

    return d->skillPoints.toInt();
}

void CharacterClass::setSkillPoints(int skillPoints)
{
    d->skillPoints = skillPoints;
}

QList<Skill *> CharacterClass::classSkills() const
{
    SkillContainer* skills = findChild<SkillContainer*>("", Qt::FindDirectChildrenOnly);
    if (!skills)
    {
        return QList<Skill*>();
    }

    return skills->items();
}

void CharacterClass::addClassSkill(const Skill &skill)
{
    SkillContainer* skills = classSkillsContainer();
    if (!skills)
    {
        return;
    }

    skills->addItem(skill);
}

void CharacterClass::removeClassSkill(int at)
{
    SkillContainer* skills = classSkillsContainer();
    if (!skills)
    {
        return;
    }

    skills->removeItem(at);
}

QList<SpecialAbility *> CharacterClass::specialAbilities(int level) const
{
    foreach (SpecialAbilityContainer* specials, findChildren<SpecialAbilityContainer*>("", Qt::FindDirectChildrenOnly))
    {
        if (specials->level() == level)
        {
            return specials->items();
        }
    }

    return QList<SpecialAbility*>();
}

void CharacterClass::addSpecialAbility(int level, const SpecialAbility &special)
{
    SpecialAbilityContainer* specials = specialAbilityContainer(level);
    if (!specials)
    {
        return;
    }

    specials->addItem(special);
}

void CharacterClass::specialAbility(int level, int at)
{
    SpecialAbilityContainer* specials = specialAbilityContainer(level);
    if (!specials)
    {
        return;
    }

    specials->removeItem(at);
}


SkillContainer *CharacterClass::classSkillsContainer()
{
    SkillContainer* skills = findChild<SkillContainer*>("", Qt::FindDirectChildrenOnly);
    if (!skills)
    {
        return new SkillContainer(QT_TR_NOOP("Class Skills"), this);
    }

    return skills;
}

SpecialAbilityContainer *CharacterClass::specialAbilityContainer(int level)
{
    foreach (SpecialAbilityContainer* specials, findChildren<SpecialAbilityContainer*>("", Qt::FindDirectChildrenOnly))
    {
        if (specials->level() == level)
        {
            return specials;
        }
    }

    return new SpecialAbilityContainer(level, this);
}

